package at.fhj.itm11.forum.data.impl;

import java.util.List;

import javax.faces.bean.ApplicationScoped;
import javax.inject.Inject;
import javax.inject.Named;
import javax.persistence.EntityManager;
import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Root;

import at.fhj.itm11.forum.data.IconDAO;
import at.fhj.itm11.forum.model.Icon;

@ApplicationScoped
@Named
public class IconDAOImpl implements IconDAO {

	@Inject
    private EntityManager em;

	@Override
	public List<Icon> retrieveAllIconsOrderedByFileName() {
		CriteriaBuilder cb = em.getCriteriaBuilder();
		CriteriaQuery<Icon> criteria = cb.createQuery(Icon.class);
		Root<Icon> icon = criteria.from(Icon.class);
		criteria.select(icon).orderBy(cb.asc(icon.get("fileName")));
		return em.createQuery(criteria).getResultList();		
	}
	
	@Override
	public Icon findIconById(Long id) {
        return em.find(Icon.class, id);
    }
}